package com.scuec.groupfive.service.impl;

import com.scuec.groupfive.mapper.WrongQuestionMapper_z;
import com.scuec.groupfive.service.WrongQuestionService_z;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WrongQuestionServiceImpl_z implements WrongQuestionService_z {

    private static final Logger logger = LoggerFactory.getLogger(WrongQuestionServiceImpl_z.class);

    @Autowired
    private WrongQuestionMapper_z wrongQuestionMapper_z;

    @Override
    public Map<String, Object> getWeakPointsByUserId(Long userId, Integer subjectId, Integer page, Integer size) {
        Map<String, Object> result = new HashMap<>();
        try {
            logger.info("Getting weak points for user: {}, subject: {}, page: {}, size: {}", 
                userId, subjectId, page, size);

            if (userId == null) {
                logger.error("User ID cannot be null");
                result.put("code", 400);
                result.put("msg", "User ID cannot be null");
                return result;
            }

            // Calculate offset
            int offset = (page - 1) * size;
            logger.debug("Calculated offset: {}", offset);

            // Get weak points
            List<Map<String, Object>> weakPoints = wrongQuestionMapper_z.getWeakPointsByUserId(userId, subjectId, offset, size);
            logger.info("Retrieved {} weak points", weakPoints.size());

            // Get total count
            int total = wrongQuestionMapper_z.getWeakPointsCount(userId, subjectId);
            logger.info("Total weak points count: {}", total);

            // Log the first weak point for debugging
            if (!weakPoints.isEmpty()) {
                logger.debug("First weak point data: {}", weakPoints.get(0));
            }

            result.put("code", 200);
            result.put("msg", "获取薄弱知识点成功");
            result.put("data", new HashMap<String, Object>() {{
                put("list", weakPoints);
                put("total", total);
            }});
        } catch (Exception e) {
            logger.error("Error getting weak points: ", e);
            result.put("code", 500);
            result.put("msg", "获取薄弱知识点失败: " + e.getMessage());
        }
        return result;
    }
} 